package chapter21;


import java.util.*;

/**
 * @program: exercise
 * @description: 1.测试一个元素是否在一个散列集、链式散列集、树形集、数组线性表中、以及链表中。
 *               2.从一个散列集、链式散列集、树形集、数组线性表中、以及链表中删除元素的执行时间。
 * @author: 陈庆彪
 * @create: 2020-02-24 09:28
 **/
public class SetListPerformanceTest {
    static final int N= 50000;

    public static void main(String[] args) {
        List<Integer> list=new ArrayList<>();
        for (int i = 0; i <N ; i++)
            list.add(i);
            Collections.shuffle(list);//弄乱list集合中元素排序

        Collection<Integer> set1=new HashSet<>(list);
        System.out.println("member test time for hash set is "+getTestTime(set1)+"milliseconds");
        System.out.println("Remove element time for hash set is "+getRemoveTime(set1)+"milliseconds");

        new LinkedHashSet<>();
        new TreeSet<>();
        new ArrayList<>();
        new LinkedList<>();


    }

    private static long getRemoveTime(Collection<Integer> c) {
        long startTime=System.currentTimeMillis();
        for (int i = 0; i <N; i++)
            c.contains(Math.random()*2*N);
        return System.currentTimeMillis()-startTime;

    }

    private static long getTestTime(Collection<Integer> c) {
        long startTime=System.currentTimeMillis();
        for (int i = 0; i <N; i++)
            c.remove(i);
        return System.currentTimeMillis()-startTime;
    }
}
